package com.xuelang.pipeline.infrastructure.resource.repository;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.resource.dto.DictQuery;
import com.xuelang.pipeline.domain.resource.Dict;
import com.xuelang.pipeline.domain.resource.repository.DictRepository;
import com.xuelang.pipeline.infrastructure.resource.converter.DictToDictDOConverter;
import com.xuelang.pipeline.infrastructure.resource.dataobject.DictDO;
import com.xuelang.pipeline.infrastructure.resource.dataobject.SpaceDO;
import com.xuelang.pipeline.infrastructure.resource.mapper.DictMapper;
import dm.jdbc.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 字典仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class DictRepositoryImpl implements DictRepository {

    @Autowired
    DictMapper dictMapper;

    @Override
    public PageRecord<Dict> pageList(long currentPage, long pageSize, DictQuery dictQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<Dict> page = new Page<>(currentPage, pageSize);
        MPJLambdaWrapper<DictDO> wrapper = new MPJLambdaWrapper<DictDO>();
        if (dictQuery != null) {
            wrapper.selectAll(DictDO.class)
                    .like(StrUtil.isNotBlank(dictQuery.getDictCode()), DictDO::getDictCode, dictQuery.getDictCode())
                    .like(StrUtil.isNotBlank(dictQuery.getDictName()), DictDO::getDictName, dictQuery.getDictName())
                    .eq(StrUtil.isNotBlank(dictQuery.getDictType()), DictDO::getDictType, dictQuery.getDictType())
                    .eq((dictQuery.getStatus() != null), DictDO::getStatus, dictQuery.getStatus())
                    .orderByDesc(SpaceDO::getCreateTime);

        }
        dictMapper.selectJoinPage(page, Dict.class, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords());
    }

    @Override
    public List<Dict> list(DictQuery dictQuery) {
        MPJLambdaWrapper<DictDO> wrapper = new MPJLambdaWrapper<DictDO>();
        if (dictQuery != null) {
            wrapper.selectAll(DictDO.class)
                    .like(StrUtil.isNotBlank(dictQuery.getDictCode()), DictDO::getDictCode, dictQuery.getDictCode())
                    .like(StrUtil.isNotBlank(dictQuery.getDictName()), DictDO::getDictName, dictQuery.getDictName())
                    .eq(StrUtil.isNotBlank(dictQuery.getDictType()), DictDO::getDictType, dictQuery.getDictType())
                    .eq((dictQuery.getStatus() != null), DictDO::getStatus, dictQuery.getStatus())
                    .orderByAsc(DictDO::getDictSort);

        }
        return dictMapper.selectJoinList(Dict.class, wrapper);
    }

    @Override
    public Boolean save(Dict dict) {
        return dictMapper.insert(DictToDictDOConverter.INSTANCE.dictToDictDO(dict)) == 1;
    }

    @Override
    public Boolean update(Dict dict) {
        return dictMapper.update(DictToDictDOConverter.INSTANCE.dictToDictDO(dict), Wrappers.lambdaQuery(DictDO.class).eq(DictDO::getId, dict.getId())) == 1;
    }

    @Override
    public Boolean delete(Long id) {
        return dictMapper.delete(Wrappers.lambdaQuery(DictDO.class).eq(DictDO::getId, id)) == 1;
    }

    @Override
    public Dict query(Long id) {
        DictDO dictDO = dictMapper.selectOne(Wrappers.lambdaQuery(DictDO.class).eq(DictDO::getId, id));
        return DictToDictDOConverter.INSTANCE.dictDOToDict(dictDO);
    }

    @Override
    public Boolean existsCode(String dictType, String dictCode) {
        List<DictDO> dictDOS = dictMapper.selectList(Wrappers.lambdaQuery(DictDO.class).eq(DictDO::getDictCode, dictCode));
        if (ObjectUtil.isEmpty(dictDOS)) {
            return false;
        }
        return dictDOS.stream().anyMatch(dictDO -> !StringUtil.equals(dictType, dictDO.getDictType()));
    }

}
